using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

class RenderDepthBufferPass : ScriptableRenderPass
{
    private static readonly string k_RenderTag = "Render Depth Buffer";
    
    private Material m_material;
    private ScriptableRenderer m_renderer;

    public RenderDepthBufferPass(RenderPassEvent evt, Material material)
    {
        renderPassEvent = evt;
        m_material = material;
    }

    public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
    {
        if (null == m_material)
        {
            Debug.LogError("RenderDepthBufferPass 找不到材质");
            return;
        }
            
        CommandBuffer cmd = CommandBufferPool.Get(k_RenderTag);

        // var source = m_renderer.cameraColorTargetHandle;
        // var destination = TempTargetID;
        //
        // var w = cameraData.camera.scaledPixelWidth;
        // var h = cameraData.camera.scaledPixelHeight;

        // cmd.GetTemporaryRT(destination, w, h, 0, FilterMode.Point, RenderTextureFormat.Default);
        // cmd.Blit(source, destination, m_material);
        // cmd.Blit(destination, source);

        var cameraTarget = m_renderer.cameraColorTargetHandle;
        cmd.Blit(null,cameraTarget, m_material);

        context.ExecuteCommandBuffer(cmd);
        CommandBufferPool.Release(cmd);
    }

    public void Setup(ScriptableRenderer renderer)
    {
        m_renderer = renderer;
    }
}